<!-- 专题页管理 -->
<template>
  <div>
    <CommonSearch @search="query" @reset="reset">
      <el-col :span="8">
        <el-form-item label="活动名称">
          <el-input
            v-model="queryParams.condition.nickname"
            clearable
            style="width: 100%"
          />
        </el-form-item>
      </el-col>

      <el-col :span="8">
        <el-form-item label="是否启用">
          <DictSelect
            v-model="queryParams.condition.enable"
            :defaultOptions="options.yesno"
            placeholder="请选择状态"
            clearable
            style="width: 100%"
          />
        </el-form-item>
      </el-col>
    </CommonSearch>

    <el-row class="cardMiniBar">
      <el-col :span="24">
        <div class="fr">
          <el-button type="primary" @click="add" size="small">新增</el-button>
          <!-- <el-button @click="onExport" size="small">导出</el-button> -->
        </div>
      </el-col>
    </el-row>

    <div class="cardBody">
      <edu-table
        ref="mainTable"
        :data="tableList"
        :pageSize="queryParams.pageSize"
        :pageNum="queryParams.pageNum"
        :total="queryParams.total"
        @page-change="handleCurrentChange"
        :order="true"
        :selection="true"
      >
        <el-table-column label="活动名称" prop="nickname" />
        <el-table-column label="创建时间" prop="createdTime" />
        <el-table-column label="是否启用" prop="enable">
          <template slot-scope="scope">
            {{ renderOption(scope.row.enable, "yesno") }}
          </template>
        </el-table-column>

        <el-table-column label="操作" fixed="right" width="220px">
          <template slot-scope="scope">
            <el-button
              type="text"
              
              @click="edit(scope.row)"
              size="small"
            >
              编辑
            </el-button>
            <el-button
              type="text"
              @click="handlerDelete(scope.row)"
              size="small"
            >
              删除
            </el-button>

            <el-button type="text" :disabled="scope.row.enable == 1" @click="disposition(scope.row)" size="small"
              >配置专题页</el-button
            >
          </template>
        </el-table-column>
      </edu-table>
    </div>

    <edu-dialog
      :title.sync="dialogConfig.title"
      :visible.sync="dialogConfig.showDialog"
      @close="handleDialogClose"
    >
      <el-form
        ref="dialogForm"
        :model="dialogConfig.formData"
        :rules="dialogConfig.action === 'view' ? {} : dialogConfig.rules"
        label-width="130px"
        size="small"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="活动名称" prop="nickname">
              <el-input v-model="dialogConfig.formData.nickname" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="enable">
              <DictSelect
                v-model="dialogConfig.formData.enable"
                :defaultOptions="options.yesno"
                placeholder="请选择状态"
                clearable
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="buttons" v-if="dialogConfig.action !== 'view'">
        <template>
          <el-button @click="closeDialog(dialogConfig)"> 取消 </el-button>
          <el-button type="primary" @click="handlerSubmit"> 提交 </el-button>
        </template>
      </span>
      <span slot="buttons" v-else>
        <template>
          <el-button @click="closeDialog(dialogConfig)">返回</el-button>
        </template>
      </span>
    </edu-dialog>
    <Disposition ref="disposition" />
  </div>
</template>

<script>
import utils from "@/utils";
import mixins from "@/mixins";
import Disposition from "./components/Disposition";
import {
  pageThematicGame,
  addThematicGame,
  updateThematicGame,
  deleteThematicGame,
} from "@/api/thematicManagement";

const formData = {
  createdTime: "", //创建时间
  enable: "", //是否启用：1.是；2.否
  id: "", //主键
  image: "", //图片文件id
  nickname: "", //活动名称
  publish: "", //是否发布：1.是；2.否
  title: "", //活动标题
};
export default {
  mixins: [mixins],
  components: {
    Disposition,
  },
  data() {
    return {
      queryParams: {
        condition: {},
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      tableList: [],
      dialogConfig: {
        title: "",
        showDialog: false,
        action: "add",
        formData: {},
        rules: {
          nickname:[
            {
              required: true,
              message: "此项必填！",
              trigger: 'blur',
            },
          ],
          enable:[
            {
              required: true,
              message: "此项必填！",
              trigger: 'blur',
            },
          ]
        },
      },
      options: {},
    };
  },
  created() {
    this.initOptions();
  },
  methods: {
    async initOptions() {
      // 查询列表
      this.query();
    },
    // 查询
    async query(pageNum) {
      // 有pageNum 重置为第一页
      if(pageNum){
        this.queryParams.pageNum = pageNum
      }
      
      let { returnObj } = await pageThematicGame(this.queryParams);
      this.tableList = returnObj.list;
      this.queryParams.total = returnObj.total;
      this.queryParams.pageSize = returnObj.pageSize;
    },
    // 重置
    reset() {
      for (var key in this.queryParams.condition) {
        this.queryParams.condition[key] = "";
      }
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;

      this.query();
    },
    // 分页
    handleCurrentChange(row) {
      this.queryParams.pageSize = row.pageSize;
      this.queryParams.pageNum = row.pageNum;

      this.query();
    },
    closeDialog(dialog) {
      dialog.showDialog = false;
      this.$refs["dialogForm"].resetFields();
    },
    openDialog(dialog) {
      dialog.showDialog = true;
    },
    // 新增
    add() {
      this.dialogConfig.title = "新增";
      this.dialogConfig.action = "add";
      this.dialogConfig.formData = utils.clone(formData);

      this.openDialog(this.dialogConfig);
    },
    // 修改
    async edit(row) {
      this.dialogConfig.title = "编辑";
      this.dialogConfig.action = "edit";

      this.dialogConfig.formData = utils.clone(row);

      this.openDialog(this.dialogConfig);
    },
    // 保存提交
    handlerSubmit() {
      let params = {
        ...this.dialogConfig.formData,
      };
      this.$refs["dialogForm"].validate((valid) => {
        if (valid) {
          if (this.dialogConfig.action == "add") {
            addThematicGame(params).then((res) => {
              this.$message({
                type: "success",
                message: "成功",
              });
              this.closeDialog(this.dialogConfig);
              this.query();
            });
          } else {
            updateThematicGame(params).then((res) => {
              this.$message({
                type: "success",
                message: "成功",
              });
              this.closeDialog(this.dialogConfig);
              this.query();
            });
          }
        }
      });
    },
    // 查看
    view() {
      this.dialogConfig.title = "查看";
      this.dialogConfig.action = "view";
      this.openDialog(this.dialogConfig);
    },
    // 关闭弹框
    handleDialogClose() {
      this.closeDialog(this.dialogConfig);
    },
    // 删除
    async handlerDelete(row) {
      let idField = "id";
      if (row && row[idField]) {
        this.batchDelete(deleteThematicGame, idField, [row[idField]]);
      } else {
        this.batchDelete(deleteThematicGame, idField);
      }
    },
    async disposition(row) {
      this.$refs.disposition.open(row);
    },
  },
};
</script>

<style lang="scss" scoped></style>
